package com.acoreful.ackitcloud.controller;

import cn.hutool.core.date.DateUtil;
import com.acoreful.ackitcloud.api.UmsAdminApi;
import com.acoreful.ackitcloud.config.MyDemoProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequiredArgsConstructor
public class NacosController{

    private final LoadBalancerClient loadBalancerClient;
    private final RestTemplate restTemplate;

    private final MyDemoProperties myDemoProperties;


    @Value("${spring.application.name}")
    private String appName;

    private final UmsAdminApi umsAdminApi;

    @GetMapping("/echo/app-name")
    public String echoAppName(){
        //Access through the combination of LoadBalanceClient and RestTemplate
        ServiceInstance serviceInstance = loadBalancerClient.choose("acfkit-cloud-gateway");
        String path = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
        log.info("request path:{}",path);
        return restTemplate.getForObject(path,String.class);
    }
    @GetMapping("/adminLogin")
    public String adminLogin(){
        log.info("now:{}", DateUtil.now());
        return umsAdminApi.login("小米","特色");
    }

    @GetMapping("/test")
    public String test(){
        log.info("name:{}", myDemoProperties.getName());
        return umsAdminApi.login("小米","特色");
    }


}